/*
 * Copyright 2006-2007 S W McIlroy
 * Licensed  under the  Apache License,  Version 2.0  (the "License");
 * you may not use  this file  except in  compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed  under the  License is distributed on an "AS IS" BASIS,
 * WITHOUT  WARRANTIES OR CONDITIONS  OF ANY KIND, either  express  or
 * implied.
 *
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.mockfu;

/**
 * Thrown when a {@link Mocks#assertThat(boolean)} or {@link Mocks#assertThat(Object, Object)}
 * assertion fails.
 */
public class AssertionFailure extends AssertionError
{
    private static final long serialVersionUID = 7544327286422990072L;

    public AssertionFailure(String message)
    {
        super(message);
    }

    public AssertionFailure(Object actual, Object constraint, Matcher<Object> matcher)
    {
        super(getMessage(actual, constraint, matcher));
    }

    private static String getMessage(Object actual, Object constraint, Matcher<Object> matcher)
    {
        Class<?> type = Constraints.argumentType(constraint == null ? actual : constraint);
        String typeName = type.getSimpleName();
        if (type == CollectionKey.class)
            typeName = "Collection";

        return "\nExpected: " + typeName + " " + matcher.description() + "\nEncountered: "
                + Constraints.formatResult(actual, matcher);

    }
}
